package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.tour.config.ResponseWrapper;
import com.tour.entity.DFood;
import com.tour.entity.Restaurant;
import com.tour.mapper.DFoodMapper;
import com.tour.service.DFoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-30
 */
@Service
public class DFoodServiceImpl extends ServiceImpl<DFoodMapper, DFood> implements DFoodService {

    @Autowired
    DFoodMapper foodMapper;

    public ResponseWrapper addFood(DFood food){
        if (food == null){
            return ResponseWrapper.markError();
        }else {
            int result = foodMapper.insert(food);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper updateFood(DFood food){
        if (food == null){
            return ResponseWrapper.markError();
        }else {
            int result = foodMapper.updateById(food);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper deleteFood(Integer fid){
        if (fid == null){
            return ResponseWrapper.markError();
        }else {
            int result = foodMapper.deleteById(fid);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findFood(Integer fid){
        if (fid == null){
            return ResponseWrapper.markError();
        }else {
            DFood food = foodMapper.selectById(fid);
            if (food != null){
                ResponseWrapper wrapper = ResponseWrapper.markSuccess(food);
                wrapper.getData().put("data",food);
                return wrapper;
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findAllfood(String name,Integer pageSize,Integer pageNum,String flavor){
        Page<DFood> page = new Page<>(pageNum,pageSize);
        QueryWrapper<DFood> restaurantQueryWrapper = new QueryWrapper<>();
        restaurantQueryWrapper.like(!StringUtils.isNullOrEmpty(name),"f_name",name).eq(!StringUtils.isNullOrEmpty(flavor),"flavor",flavor);
        IPage<DFood> iPage = foodMapper.selectPage(page,restaurantQueryWrapper);
        List<DFood> list = iPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("data",list);
        wrapper.getData().put("total",iPage.getTotal());
        wrapper.getData().put("pageSize",iPage.getSize());
        wrapper.getData().put("pageNum",iPage.getCurrent());
        return wrapper;
    }
}
